﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace DetectingKinectSencor
{
    /// <summary>
    /// The following class was provided by the book 
    /// Microsoft.Press.Programming.with.the.Kinect.for.Windows.Software.Development.Kit.Sep.2012
    /// </summary>
    public abstract class Notifier : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// As you can see, this class uses an expression to detect the name of the property to signal. This is 
        /// quite useful, because with this technique you don’t have to pass a string (which is hard to keep in sync 
        /// with your code when, for example, you rename your properties) to define your property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyExpression"></param>
        
        protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            var memberExpression = propertyExpression.Body as MemberExpression;
            
            if (memberExpression == null)
                return;
            
            string propertyName = memberExpression.Member.Name;
           
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}